//
//  UserAuthPickerViewController.m
//  nonghubao
//
//  Created by chongqing642 on 13-2-22.
//  Copyright (c) 2013年 mypocketbaby. All rights reserved.
//

#import "UserAuthPickerViewController.h"
#import "Config.h"
#import "Helper.h"

////用户认证的类型
//#define DEFAULT                             @"未认证"
//#define RURAL_MICRO_ENTERPRISE              @"农村微企业"
//#define GOVERNMENT_SUPPORTER                @"政府帮扶者"
//#define GOVERNMENT_ADMINISTRATOR            @"政府管理员"
//#define PLATFORM_ADMINISTRATOR              @"平台管理员"


@interface UserAuthPickerViewController ()

@end

@implementation UserAuthPickerViewController
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewStyle)style withAuthType:(NSString *)authType
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        _markedAuthType = authType;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    self.tableView.backgroundView = nil;
    
    //设置不可滚动
    self.tableView.scrollEnabled = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    // Return the number of rows in the section.
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //已认证图标
        UIImageView *markedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 14.75f, 35.0f, 14.5f)];
        
        
        [cell.contentView addSubview:markedImageView];
        
        //用户认证类型
        UILabel *authCategoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 12.0f, 140.0f, 20.0f)];
        authCategoryLabel.font = _SYS_FONT_007;
        authCategoryLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:authCategoryLabel];
    }
    
    //取得cell中的子view
    
    //用户认证类型
    UILabel *authCategoryLabel = [[cell.contentView subviews] objectAtIndex:1];
    
    if (indexPath.row == 0) {
        
        authCategoryLabel.text = _L(@"title_27");   //认证为农村微企业
    }else if (indexPath.row == 1){
        
        authCategoryLabel.text = _L(@"title_28");   //认证为政府帮扶者
    }else if (indexPath.row == 2){
        
        authCategoryLabel.text = _L(@"title_29");   //认证为政府管理员
    }else if (indexPath.row == 3){
        
        authCategoryLabel.text = _L(@"title_30");   //认证为平台管理员
    }
    
    //标记用户认证
    if ([authCategoryLabel.text isEqualToString:[NSString stringWithFormat:_L(@"title_31"), _markedAuthType]]) {   //认证为%@
        
        //已认证图标
        UIImageView *markedImageView = [[cell.contentView subviews] objectAtIndex:0];
        markedImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_icon_003@2x" ofType:@"png"]];
        
        //将次cell标记为下次要取消
        _cellShouldBeUnmark = indexPath;
    }
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获得以前被标记的cell
    UITableViewCell *cellShouldBeUnmark = [self.tableView cellForRowAtIndexPath:_cellShouldBeUnmark];
    UIImageView *unmarkImageView = [[cellShouldBeUnmark.contentView subviews] objectAtIndex:0];
    unmarkImageView.image = nil;

    
    //获得被点击cell中的label的值
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    //标记被点击的cell
    UIImageView *markedImageView = [[selectedCell.contentView subviews] objectAtIndex:0];
    markedImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_icon_003@2x" ofType:@"png"]];

    //将次cell标记为下次要取消
    _cellShouldBeUnmark = indexPath;
    
    //用户认证类型
    UILabel *authCategoryLabel = [[selectedCell.contentView subviews] objectAtIndex:1];
    
    if ([_delegate respondsToSelector:@selector(receivingUserAuthType:)]) {
        
        [_delegate receivingUserAuthType:[authCategoryLabel.text substringFromIndex:3]];
    }
    
}

#pragma mark - 事件 

// 返回按钮事件
//
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
